package com.brest.managesambafilesharing;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import android.os.AsyncTask;

public class IsSambaRunning extends AsyncTask<String, Integer, String> { //

	OnResponseListenerIsSambaRunning responder;

	public IsSambaRunning(OnResponseListenerIsSambaRunning onResponseListener) {
		responder = onResponseListener;
	}

	public interface OnResponseListenerIsSambaRunning {
		public void onResponse(String message);
	}

	protected void onPreExecute() {
		LOG.i("Pre-Execute");
		super.onPreExecute();
	}

	// Called to initiate the background activity
	@Override
	protected String doInBackground(String... statuses) { //
		String res = "";
		String resok = "Samba is running!!!";
		String resnotok = "Samba is not running!!!";
		int i = 10;

		try {
			// publishProgress(10);
			LOG.i("Percentage of progress: " + 10);
			boolean running = false;
			int pid = -1;
			/*
			 * pid = android.os.Process
			 * .getUidForName("//data//data//com.funkyfresh.samba//files//nmbd"
			 * ); ActivityManager am = (ActivityManager)
			 * getSystemService(Context.ACTIVITY_SERVICE);
			 * List<ActivityManager.RunningAppProcessInfo> l = am
			 * .getRunningAppProcesses(); for (RunningAppProcessInfo processInfo
			 * : l) { Log.d(TAG, " " + processInfo.processName + " : " +
			 * processInfo.pid); }
			 */

			File f = new File(SambaCmdToolBean.SMBDPIBSAMBA);
			if (f.exists()) {
				StringBuilder contents = new StringBuilder();

				try {
					// use buffering, reading one line at a time
					// FileReader always assumes default encoding is OK!
					BufferedReader input = new BufferedReader(new FileReader(f));
					try {
						String line = null; // not declared within while
											// loop
						/*
						 * readLine is a bit quirky : it returns the content of
						 * a line MINUS the newline. it returns null only for
						 * the END of the stream. it returns an empty String if
						 * two newlines appear in a row.
						 */
						while ((line = input.readLine()) != null) {
							contents.append(line);
							contents.append(System
									.getProperty("line.separator"));
						}
					} finally {
						input.close();
					}
				} catch (IOException ex) {
					ex.printStackTrace();
				}

				pid = Integer.parseInt(contents.toString().trim());
				running = true;
			} else {
				pid = -1;
			}

			// int pid = android.os.("nmbd");
			// ServiceManager.getService("window");
			LOG.d("smbd pid : " + pid);

			f = new File(SambaCmdToolBean.NMBDPIBSAMBA);
			if (f.exists()) {
				StringBuilder contents = new StringBuilder();

				try {
					// use buffering, reading one line at a time
					// FileReader always assumes default encoding is OK!
					BufferedReader input = new BufferedReader(new FileReader(f));
					try {
						String line = null; // not declared within while
											// loop
						/*
						 * readLine is a bit quirky : it returns the content of
						 * a line MINUS the newline. it returns null only for
						 * the END of the stream. it returns an empty String if
						 * two newlines appear in a row.
						 */
						while ((line = input.readLine()) != null) {
							contents.append(line);
							contents.append(System
									.getProperty("line.separator"));
						}
					} finally {
						input.close();
					}
				} catch (IOException ex) {
					ex.printStackTrace();
				}

				pid = Integer.parseInt(contents.toString().trim());
			} else {
				pid = -1;
			}

			// int pid = android.os.("nmbd");
			// ServiceManager.getService("window");
			LOG.d("nmbd pid : " + pid);
			if (running) {
				res = resok;
			} else {
				res = resnotok;
			}
			LOG.i(res);

		} catch (Exception e) {
			LOG.e(e.toString());
			e.printStackTrace();
			res = "Failed to doInBackground";
		}
		return res;
	}

	// Called when there's a status to be updated
	@Override
	protected void onProgressUpdate(Integer... progress) { //
		LOG.i("Progress Update: " + progress[0].toString());
		super.onProgressUpdate(progress[0]);
		// SambaFileSharingUpdate.this.mProgressBarSearchSamba
		// .setProgress(progress[0]);
		// SambaFileSharingUpdate.this.mProgressBar.
	}

	// Called once the background activity has completed
	@Override
	protected void onPostExecute(String result) { //
		if (responder != null) {
			responder.onResponse(result);
		}
	}
}
